package com.motu.vertx.module.utility.webhandler;

import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import com.motu.vertx.module.utility.webtool.SearchCondition;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.jdbcclient.JDBCPool;
import io.vertx.sqlclient.Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
public class GmaccountHandler extends BaseWebHandler{
	private final static Logger logger = LoggerFactory.getLogger(GmaccountHandler.class);
	private static final String tableName = "gm_account";
	private static final String tableName2 = "gm_hosteduser";

	private static Pool jdbcClientServer = null;

	public static void init(Pool jdbcServer){
		jdbcClientServer = jdbcServer;
	}
	public static void list(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getTableList(routingContext,jdbcClientServer, tableName);
	}
	public static void add(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		addToTable(routingContext, jdbcClientServer, tableName, null);
	}
	
	public static void update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		updateToTable(routingContext, jdbcClientServer, tableName, null);
	}
	
	/*
	 * 战国机甲乱3
	 */
	public static void getone(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		HttpServerResponse response = routingContext.response();
		int id = json.getInteger("id");
		String sql = "select * FROM "+tableName+" where id = ?;";
		JsonArray params = new JsonArray();
		params.add(id);
		MysqlTool.queryJsonArrayFromDb(jdbcClientServer, sql, params, handler->{
			if(handler.succeeded()){
				JsonArray list = handler.result();
				if(list.size() == 1){
					String sql2 =  "select * FROM "+tableName2+" where accountid = ? and platform = ?;";
					JsonArray params2 = new JsonArray();
					JsonObject obj = list.getJsonObject(0);
					params2.add(obj.getString("accountid"));
					params2.add(obj.getInteger("platform"));
					MysqlTool.queryJsonArrayFromDb(jdbcClientServer, sql2, params2, handler2->{
						if(handler2.succeeded()){
							JsonArray list2 = handler2.result();
							JsonObject result = new JsonObject();
							obj.put("hostusers", list2);
							result.put("info",obj);
							sendHttpResponse(response, result);
						}else{
							logger.error(handler2.cause().getMessage());
							sendHttpResponseError(response, "数据库查询失败，请稍后重试！");
						}
					});

				}else{
					sendHttpResponseError(response, "数据库查询失败，请输入有效的id！");
				}

			}else{
				logger.error(handler.cause().getMessage());
				sendHttpResponseError(response, "数据库查询失败，请稍后重试！");
			}
		});
	}
	
	
	/*
	 * 首富医院项目使用
	 */
	public static void getone2(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		HttpServerResponse response = routingContext.response();
		int id = json.getInteger("id");
		String sql = "select * FROM "+tableName+" where id = ?;";
		JsonArray params = new JsonArray();
		params.add(id);
		MysqlTool.queryJsonArrayFromDb(jdbcClientServer, sql, params, handler->{
			if(handler.succeeded()){
				JsonArray list = handler.result();
				if(list.size() == 1){
					String sql2 =  "select * FROM "+tableName2+" where accountid = ? and accounttype = ?;";
					JsonArray params2 = new JsonArray();
					JsonObject obj = list.getJsonObject(0);
					params2.add(obj.getString("accountid"));
					params2.add(obj.getInteger("accounttype"));
					MysqlTool.queryJsonArrayFromDb(jdbcClientServer, sql2, params2, handler2->{
						if(handler2.succeeded()){
							JsonArray list2 = handler2.result();
							JsonObject result = new JsonObject();
							obj.put("hostusers", list2);
							result.put("info",obj);
							sendHttpResponse(response, result);
						}else{
							logger.error(handler2.cause().getMessage());
							sendHttpResponseError(response, "数据库查询失败，请稍后重试！");
						}
					});
				}else{
					sendHttpResponseError(response, "数据库查询失败，请输入有效的id！");
				}
			}else{
				logger.error(handler.cause().getMessage());
				sendHttpResponseError(response, "数据库查询失败，请稍后重试！");
			}
		});
	}
	
	public static void hosteduser_update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		updateToTable(routingContext, jdbcClientServer, tableName2, null);
	}
	public static void hosteduser_add(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		addToTable(routingContext, jdbcClientServer, tableName2, null);
	}
	public static void hosteduser_delete(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		deleteFromTable(routingContext, jdbcClientServer, tableName2);
	}
	
	
	/*
	 * 战国机甲乱3
	 */
	public static void delete(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		HttpServerResponse response = routingContext.response();
		String accountId = json.getString("accountid");
		int platform = json.getInteger("platform");
		logger.debug("delete accountId:"+accountId+" platform:"+platform);
		String sql = "delete from "+tableName+"  where accountid = ? and platform = ?;";
		JsonArray params = new JsonArray();
		params.add(accountId);
		params.add(platform);
		MysqlTool.updateToDb(jdbcClientServer, sql, params, handler->{
			if(handler.succeeded()){
				String sql2 = "delete from "+tableName2+"  where accountid = ? and platform = ?;";
				MysqlTool.updateToDb(jdbcClientServer, sql2, params, handler2->{
					if(handler2.failed()){
						logger.error(handler2.cause().getMessage());
					}
				});
				JsonObject result = new JsonObject();

				sendHttpResponse(response, result);
			}else{
				logger.error(handler.cause().getMessage());
				sendHttpResponseError(response, "数据库更新失败，请稍后重试！");
			}
		});
	}
	
	/*
	 * 首富
	 */
	public static void delete2(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		HttpServerResponse response = routingContext.response();
		String accountId = json.getString("accountid");
		int accounttype = json.getInteger("accounttype");
		logger.debug("delete accountId:"+accountId+" accounttype:"+accounttype);
		String sql = "delete from "+tableName+"  where accountid = ? and accounttype = ?;";
		JsonArray params = new JsonArray();
		params.add(accountId);
		params.add(accounttype);
		MysqlTool.updateToDb(jdbcClientServer, sql, params, handler->{
			if(handler.succeeded()){
				String sql2 = "delete from "+tableName2+"  where accountid = ? and accounttype = ?;";
				MysqlTool.updateToDb(jdbcClientServer, sql2, params, handler2->{
					if(handler2.failed()){
						logger.error(handler2.cause().getMessage());
					}
				});
				JsonObject result = new JsonObject();
				sendHttpResponse(response, result);
			}else{
				logger.error(handler.cause().getMessage());
				sendHttpResponseError(response, "数据库更新失败，请稍后重试！");
			}
		});
	}
	
	/*
	 * 查询
	 */
	public static void search(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		scList.add(new SearchCondition("key", "like", new String[]{"accountid"}, null));
		searchFromTable(routingContext, jdbcClientServer, json, tableName, scList);
	}
}
